//
//  RegisterNewAccountWithEmailViewController.m
//  insyte
//
//  Created by Vu Dang Tuan on 6/21/15.
//  Copyright (c) 2015 Robert. All rights reserved.
//

#import "RegisterNewAccountWithEmailViewController.h"
#import "Constants.h"
#import "Helpers.h"
#import "UIColor+InsyteColors.h"
#import "ServiceManager.h"
#import "ProfileStep1ViewController.h"
#import "ProfilesViewController.h"

@interface RegisterNewAccountWithEmailViewController ()<UITextFieldDelegate>

@end

@implementation RegisterNewAccountWithEmailViewController
{
    NSMutableArray *_allTextfields;
    float _yAs;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpNavigationBar];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)];
    
    [self.view addGestureRecognizer:tap];
    
    [self setupTextfields];
    [Helpers StyleButtonWithBorderColor:_btnRegister color:[UIColor loginButtonColor] corner:0];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];
    _yAs = self.view.frame.origin.y;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)setUpNavigationBar{
    UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:Ic_Back] style:UIBarButtonItemStylePlain target:self action:@selector(OnBackClicked)];
    self.navigationItem.leftBarButtonItem = btnBack;
    self.navigationItem.titleView = nil;
}
- (void) OnBackClicked{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void) setupTextfields
{
    _allTextfields = [[NSMutableArray alloc] initWithArray:@[_tfFirstName, _tfLastName, _tfEmail, _tfPass, _tfConfirmPass]];
    for (UITextField *tf in _allTextfields) {
        tf.delegate = self;
    }
}


- (IBAction)OnRegisterClicked:(id)sender {
    if (![Helpers isAllTextFieldNotEmpty:_allTextfields]) {
        return;
    }
    if (![Helpers validateEmail:_tfEmail.text]) {
        [Helpers showAnnouncementDialog:INVALID_EMAIL];
        return;
    }
    if (_tfPass.text.length < 6) {
        [Helpers showAnnouncementDialog:PASSWORD_LENGHT];
        return;
    }
    
    if (![_tfPass.text isEqualToString:_tfConfirmPass.text]) {
        [Helpers showAnnouncementDialog:PASSWORD_NOT_MATCH];
        return;
    }
    [ServiceManager registerWithEmail:_tfEmail.text password:_tfPass.text firstname:_tfFirstName.text lastname:_tfLastName.text cb:^(BOOL rs) {
        if (rs) {
            [ServiceManager loginWithEmail:_tfEmail.text password:_tfPass.text cb:^(BOOL rs) {
                if (rs) {
                    // navigate to profile
                    ProfilesViewController *profile = [[ProfilesViewController alloc] init];
                    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:profile];
                    [[UIApplication sharedApplication].keyWindow setRootViewController: navController];
                }
            }];
        }
    }];
    
    
}



#pragma keyboard

- (void)keyboardDidHide: (NSNotification *) notif{
    [Helpers scrollView:self.view y:_yAs];
}
#pragma Textfield delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == _tfConfirmPass) {
        [Helpers scrollView:self.view y:_yAs - 130];
    }
    if (textField == _tfPass) {
        [Helpers scrollView:self.view y:_yAs - 90];
    }
    if (textField == _tfEmail) {
        [Helpers scrollView:self.view y:_yAs - 50];
    }
    if (textField == _tfFirstName || textField == _tfLastName) {
        [Helpers scrollView:self.view y:_yAs - 0];
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
-(void)dismissKeyboard {
    for (UITextField *tf in _allTextfields) {
        [tf resignFirstResponder];
    }
    
}
@end
